//
//  ArticleDetailController.m
//  Sth_On_The_Road
//
//  Created by LEO on 12/30/15.
//  Copyright © 2015 Leo_Lei. All rights reserved.
//

#import "ArticleDetailController.h"

@interface ArticleDetailController ()<UIWebViewDelegate>{
    UIActivityIndicatorView *_indicator;
}

@end

@implementation ArticleDetailController

#pragma mark - 系统方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = _articleModel.shortTitle;
    [self.navigationController.navigationBar setTitleTextAttributes:@{
                                                                      NSForegroundColorAttributeName:[UIColor blackColor]
                                                                      }];
    [self addNavigationBarButton];
    [self loadWebViewWithURL:_articleModel.url parameters:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 网页视图
- (void)loadWebViewWithURL:(NSString *)urlString parameters:(NSDictionary *)parameters {
    //  创建整体内容的网页视图
    UIWebView *articleWebView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    
    //  设置其网页内容自动适配
    articleWebView.scalesPageToFit = YES;
    articleWebView.delegate = self;
    
    NSURL *url = [NSURL URLWithString:urlString];
    //  请求网页数据
    NSURLRequest *dataRequest = [NSURLRequest requestWithURL:url];
    [articleWebView loadRequest:dataRequest];
    
    _indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [_indicator stopAnimating];
    
    [self.view addSubview:articleWebView];
    [self.view addSubview:_indicator];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    [_indicator startAnimating];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [_indicator stopAnimating];
}

- (void)addNavigationBarButton {
    //收藏按钮
    UIButton *lovedButton = [UIButton buttonWithType:UIButtonTypeCustom];
    lovedButton.frame = CGRectMake(0, 0, 40, 40);
    [lovedButton setImage:[UIImage imageNamed:@"heart_half_red"] forState:UIControlStateNormal];
    [lovedButton setImage:[UIImage imageNamed:@"heart_full_red"] forState:UIControlStateSelected];
    [lovedButton addTarget:self action:@selector(lovedAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *loved = [[UIBarButtonItem alloc] initWithCustomView:lovedButton];
    
    //分享按钮
    UIButton *sharedButton = [UIButton buttonWithType:UIButtonTypeCustom];
    sharedButton.frame = CGRectMake(0, 0, 40, 40);
    [sharedButton setImage:[UIImage imageNamed:@"share_red"] forState:UIControlStateNormal];
    [sharedButton addTarget:self action:@selector(sharedAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *shared = [[UIBarButtonItem alloc] initWithCustomView:sharedButton];
    self.navigationItem.rightBarButtonItems = @[loved, shared];
}

- (void)lovedAction:(UIButton *)button {
    button.selected = !button.selected;
}

- (void)sharedAction {
    NSLog(@"Hello");;
}
@end
